<template>
  <el-dialog
    title="数据录入"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="dailyDialog"
  >
    <el-form
      :inline="true"
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      @keyup.enter.native="getDataList()"
      label-width="120px"
    >
      <div class="formAddOrUpdate">
        <div class="box">
          <el-form-item label="日期时间:" prop="date">
            <el-date-picker
              v-model="dataForm.date"
              type="datetime"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间"
            />
          </el-form-item>
          <el-form-item label="流量计:" prop="areaId">
            <el-select
              v-model="dataForm.areaId"
              filterable
              @change="selectValue"
              placeholder="请选择流量计"
            >
              <el-option
                v-for="(item, index) in areaOptions"
                :key="index"
                :value="item.id"
                :label="item.flowMeterName"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="box">
          <el-form-item label="正流量行度:" prop="positiveFlow">
            <el-input
              autocomplete="off"
              v-model="dataForm.positiveFlow"
              placeholder="请输入正流量行度"
            />
          </el-form-item>
          <el-form-item label="负流量行度:" prop="negativeFlow">
            <el-input
              autocomplete="off"
              v-model="dataForm.negativeFlow"
              placeholder="请输入负流量行度"
            />
          </el-form-item>
        </div>
        <div class="box">
          <el-form-item label="瞬时流量:" prop="instantFlow">
            <el-input
              autocomplete="off"
              v-model="dataForm.instantFlow"
              placeholder="请输入瞬时流量"
            />
          </el-form-item>
        </div>
        <div class="box1">
          <el-form-item label="备注:" prop="remark">
            <el-input
              autocomplete="off"
              type="textarea"
              v-model="dataForm.remark"
            />
          </el-form-item>
        </div>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false, // 弹层的显示
      stationData: {}, // 传入的泵站信息
      dataForm: {
        id: 0,
        date: "", // 时间
        areaId: null, // 片区id
        area: null, // 片区
        positiveFlow: "", // 正流量行度
        negativeFlow: 0, // 负流量行度
        instantFlow: "", // 瞬时流量
        remark: "" // 备注
      },
      areaOptions: [],
      dataRule: {
        areaId: [{ required: true, message: "请选择流量计", trigger: "blur" }],
        positiveFlow: [
          { required: true, message: "正流量行度不能为空", trigger: "blur" }
        ],
        negativeFlow: [
          { required: true, message: "负流量行度不能为空", trigger: "blur" }
        ],
        instantFlow: [
          { required: true, message: "瞬时流量不能为空", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    this.getAreaDataList();
  },
  methods: {
    init() {
      // dateTime - 传入的年月日时分秒
      // dataGroup - 传入的泵站信息
      this.visible = true; // 显示弹层
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/cms/tbCopyFlowData/save"),
            method: "post",
            data: this.$http.adornData({
              area: this.dataForm.area, // 地点泵站
              areaId: this.dataForm.areaId, // 地点泵站id
              date: this.dataForm.date,
              positiveFlow: parseFloat(this.dataForm.positiveFlow).toFixed(2),
              negativeFlow: this.dataForm.negativeFlow
                ? parseFloat(this.dataForm.negativeFlow).toFixed(2)
                : 0,
              instantFlow: this.dataForm.instantFlow
                ? parseFloat(this.dataForm.instantFlow).toFixed(2)
                : 0,
              remark: this.dataForm.remark
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 获取数据
    selectValue(val) {
      let ChooseItem = this.areaOptions.filter(item => item.id === val);
      if (ChooseItem.length > 0) {
        this.dataForm.area = ChooseItem[0].flowMeterName;
      }
    },
    // 爬取流量计数据
    getAreaDataList() {
      this.$http({
        url: this.$http.adornUrl(`/cms/tbEquipment/combo`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.areaOptions = data.list;
        } else {
          this.areaOptions = [];
        }
      });
    }
  }
};
</script>
